package com.example.Project.user;

import com.example.Project.Class.MassOrganizations;
import com.example.Project.user.userHttpMethods.userMethods;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.time.LocalDate;

public class AddMassController {

    @FXML
    public void initialize(){
        type.getItems().addAll("功能型", "爱好型", "艺术型");
    }
    @FXML
    private TextField name;

    @FXML
    private TextField phone;

    @FXML
    private Button add;

    @FXML
    private ComboBox<String> type;
    userMethods userMethods=new userMethods();
    Stage stage=new Stage();

    @FXML
    void createMass(MouseEvent event) {
        if(name.getText().isEmpty()||phone.getText().isEmpty()||type.getSelectionModel().getSelectedItem()==null){
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setHeaderText("请将信息填写完整！！！");
            alert.getButtonTypes().add(ButtonType.YES);
            alert.showAndWait();
            return;
        }
        MassOrganizations mass=new MassOrganizations();
        if(!name.getText().isEmpty()) {
            mass.setName(name.getText());
        }
        if(!phone.getText().isEmpty()) {
            mass.setPhone(phone.getText());
        }
        if(type.getSelectionModel().getSelectedItem()!=null) {
            mass.setType( type.getSelectionModel().getSelectedItem());
        }
        mass.setBirthday(LocalDate.now());
        boolean flag=userMethods.addMassOrganization(mass);
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        if(flag){
            alert.setHeaderText("添加成功！！！");
            alert.getButtonTypes().add(ButtonType.YES);
            alert.showAndWait();
            stage=(Stage)add.getScene().getWindow();
            stage.close();
        }else{
            alert.setHeaderText("添加失败，请重新操作！！！");
            alert.getButtonTypes().add(ButtonType.YES);
            alert.showAndWait();
        }
    }

}
